package org.qiuqiu.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.qiuqiu.utils.CodeImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping(value="/code")
public class CodeController {

	String code;
	// 发送验证码图片
	@RequestMapping(value="/getImage")
	public void getImage(HttpServletResponse resp, HttpServletRequest req) throws Exception{
		code = CodeImageUtil.sendImage(resp, req);
	}
	
	// 验证验证码
	@RequestMapping(value="isCodeRight")
	public void isCodeRight(@RequestParam("code")String code_rec,HttpServletResponse resp) throws Exception{
		JSONObject data = new JSONObject();
		if(code_rec!=null && code_rec.equalsIgnoreCase(code)){
			data.put("flag", 1);
		}else{
			data.put("flag", 0);
		}
		resp.setContentType("application/json; charset=utf-8");
		PrintWriter  out = resp.getWriter();
		out.write(data.toString());
		out.flush();
		out.close();
	}
}
